//
//  SIHelpViewController.m
//  CampingLight
//
//  Created by Piccolo on 2018/6/30.
//  Copyright © 2018 SmartIT. All rights reserved.
//

#import "SIHelpViewController.h"
#import "SIHelpDetailViewController.h"

static NSArray<NSString *> *kTitleStringArray;
static NSArray<NSString *> *kDetailStringArray;

static CGFloat const kTableViewHeaderHeight = 50.f;
static CGFloat const kDefaultVerticalBounds = 8.f;
static CGFloat const kDefaultHorizontalBounds = 20.f;

@interface SIHelpViewController () <UITableViewDelegate, UITableViewDataSource> {
    
    __weak IBOutlet UITableView *_helpTableView;
}

@end

@implementation SIHelpViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupInitialization];
    [self setupNavigationBar];
    [self setupTableView];
    // Do any additional setup after loading the view from its nib.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
}

- (void)setupInitialization {
    
    kTitleStringArray = [NSArray<NSString *> arrayWithObjects:
                  [SIUtil getSingleKeyStr:@"TEXT_HELP_SUB_TITLE_I"],
                  [SIUtil getSingleKeyStr:@"TEXT_HELP_SUB_TITLE_II"],
                  [SIUtil getSingleKeyStr:@"TEXT_HELP_SUB_TITLE_III"], nil];
    kDetailStringArray = [NSArray<NSString *> arrayWithObjects:
                          [SIUtil getSingleKeyStr:@"TEXT_HELP_DESCRIPTION_I"],
                          [SIUtil getSingleKeyStr:@"TEXT_HELP_DESCRIPTION_II"],
                          [SIUtil getSingleKeyStr:@"TEXT_HELP_DESCRIPTION_III"], nil];
}

- (void)setupNavigationBar {
    
    self.navigationItem.title = [SIUtil getSingleKeyStr:@"TEXT_HELP"];
    UIBarButtonItem *backItem = [SIUtil newBarItemWithImageName:@"ico-back.png" target:self action:@selector(backBtnOnClick:)];
    self.navigationItem.leftBarButtonItem = backItem;
}

- (void)setupTableView {
    
    _helpTableView.dataSource = self;
    _helpTableView.delegate = self;
    _helpTableView.backgroundColor = [UIColor clearColor];
    _helpTableView.tableFooterView = [UIView new];
}

#pragma mark - UITableViewDataSource

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return kTitleStringArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    static NSString *simpleTableIdentifier = @"help_cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    
    if (cell == nil) {
        cell = [UITableViewCell new];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.textLabel.text = kTitleStringArray[indexPath.row];
        cell.backgroundColor = [UIColor clearColor];
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return cell;
}

#pragma mark - UITableViewDelegate

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(_helpTableView.frame), kTableViewHeaderHeight)];
    UILabel *headerLabel = [[UILabel alloc] initWithFrame:CGRectMake(kDefaultHorizontalBounds, kDefaultVerticalBounds, CGRectGetWidth(_helpTableView.frame) - kDefaultHorizontalBounds * 2 - 1, kTableViewHeaderHeight - kDefaultVerticalBounds * 2)];
    UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(kDefaultHorizontalBounds, kTableViewHeaderHeight - 1, CGRectGetWidth(_helpTableView.frame), .5f)];
    lineView.backgroundColor = [UIColor whiteColor];
    headerLabel.text = [SIUtil getSingleKeyStr:@"TEXT_HELP_TITLE"];
    headerLabel.textColor = [UIColor whiteColor];
    [headerView addSubview:headerLabel];
    [headerView addSubview:lineView];
    return headerView;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    
    return kTableViewHeaderHeight;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return 1;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SIHelpDetailViewController *helpDetailViewController = [[SIHelpDetailViewController alloc] initWithNibName:@"SIHelpDetailViewController" bundle:nil];
    helpDetailViewController.questionString = kTitleStringArray[indexPath.row];
    helpDetailViewController.answerString = kDetailStringArray[indexPath.row];
    helpDetailViewController.showQRCode = !indexPath.row;
    [self.navigationController pushViewController:helpDetailViewController animated:YES];
}

#pragma mark - Button Action

- (void)backBtnOnClick:(UIButton *)btn {
    
    [self.navigationController popViewControllerAnimated:YES];
}

@end
